package com.zhku.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhku.entity.Appointable_list;
import com.zhku.entity.Vaccinum;
import com.zhku.entity.VaccinumType;
import com.zhku.service.IAppointableListService;
import com.zhku.service.IVaccinumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: SpringBoot_School_Work
 * @BelongsPackage: com.zhku.controller
 * @Author: "deweyMDH"
 * @CreateTime: 2023-04-07  19:31
 * @Version: 1.0
 */
@RestController
@RequestMapping(value = "/appointableList")
public class AppointableListController {
    // 自动装配
    @Autowired
    IAppointableListService appointableListService;

    @Autowired
    IVaccinumService vaccinumService;

    //使用jackson来转换map
    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * @description: 可携带一个地点关键字，进行查询预约的信息
     * @author: deweyMDH
     * @param: [pageNum, pageSize, keyword]
     * @return: java.lang.String
     **/
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(
            @RequestParam(required = true, defaultValue = "1") Integer pageNum,
            @RequestParam(required = true, defaultValue = "1") Integer pageSize,
            @RequestParam(required = false, defaultValue = "") String keyword  //关键词
    ) throws JsonProcessingException {
        // 定义一个map来存放json的键值对
        Map result = new HashMap();
        //分页查询
        /*分页构造函数
        Params:
        current – 当前页
        size – 每页显示条数*/

        //查询构造条件
        QueryWrapper<Appointable_list> queryWrapper = new QueryWrapper<>();
        // 成立的表达式，字段名 和数值
        queryWrapper.like(keyword != null && !keyword.equals(""), "address", keyword);

        Page<Appointable_list> page = appointableListService.page(new Page<>(pageNum, pageSize), queryWrapper);
        page.getRecords().forEach(en -> {
            // 利用 Appointable_list 的疫苗id，查到疫苗名称，并设回 Appointable_list
            Vaccinum vaccinum = vaccinumService.getById(en.getVaccinumId());
            en.setVname(vaccinum.getName());
        });
        // 在结果中放一个list集合
        result.put("list", page.getRecords());
        result.put("total", page.getTotal());
        // 返回json
        return objectMapper.writeValueAsString(result);
    }

    /**
     * @description: 查询疫苗中的疫苗种类，提供前端选择
     * @author: deweyMDH
     * @param: []
     * @return: java.lang.String
     **/
    @RequestMapping(value = "/listType", method = RequestMethod.GET)
    public String listType() throws JsonProcessingException {
        // 定义一个map来存放json的键值对
        Map result = new HashMap();

        List<Vaccinum> list = vaccinumService.list();
        if (list != null) {
            result.put("ok", true);
            result.put("list", list);
        } else {
            result.put("ok", false);
            result.put("message", "查询疫苗数量错误");
        }

        // 返回json
        return objectMapper.writeValueAsString(result);
    }

    /**
     * @description: 新增一个预约类型
     * @author: deweyMDH
     * @param: [appointableList]
     * @return: java.lang.String
     **/
    @PostMapping(value = "/insert")
    public String insertAppointable(Appointable_list appointableList) throws JsonProcessingException {
//         定义一个map来存放json的键值对
        Map result = new HashMap();

        boolean one = appointableListService.save(appointableList);
        //判断数据库是否有数据
        if (one != false) {
            result.put("ok", true);
            result.put("appointableList", appointableList);
        } else {
            result.put("ok", false);
            result.put("message", "错误");
        }
        // 返回
        return objectMapper.writeValueAsString(result);
    }


    /**
     * @description: 修改预约记录的数据
     * @author: deweyMDH
     * @param: [appointableList]
     * @return: java.lang.String
     **/
    @PutMapping(value = "/update")
    public String updateAppointList(Appointable_list appointableList) throws JsonProcessingException {
//         定义一个map来存放json的键值对
        Map result = new HashMap();

        boolean one = appointableListService.updateById(appointableList);
        //判断数据库是否有数据
        if (one != false) {
            result.put("ok", true);
            result.put("appointableList", appointableList);
        } else {
            result.put("ok", false);
            result.put("message", "修改预约类型错误");
        }
        // 返回
        return objectMapper.writeValueAsString(result);
    }

    /**
     * @description: 根据主键，删除相应的预约类型
     * @author: deweyMDH
     * @param: [Id]
     * @return: java.lang.String
     **/
    @DeleteMapping(value = "/delete/{id}")
    public String deleteAppointList(@PathVariable(value = "id") Integer Id) throws JsonProcessingException {
//         定义一个map来存放json的键值对
        Map result = new HashMap();

        boolean one = appointableListService.removeById(Id);
        //判断数据库是否有数据
        if (one != false) {
            result.put("ok", true);
            result.put("appointableList", Id);
        } else {
            result.put("ok", false);
            result.put("message", "删除预约类型错误");
        }
        // 返回
        return objectMapper.writeValueAsString(result);
    }
}
